<?php

/**
 * 订单消息提醒列表
 * @author renjianhong
 * @date 2021-8-03 16:47
 */
namespace Modules\Order\Http\Controllers\Admin;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Modules\Order\Http\Controllers\Controller;
use Modules\Order\Entities\OrderMessage;
use Modules\Order\Entities\Order;


class OrderMessageController extends Controller
{
     /**
     * 订单消息提醒列表
     */
    public function list(Request $request)
    { 
        return view('order::admin.order_message.list');
    }

    /**
     * AJAX获取数据
     */
    public function ajaxList(Request $request , OrderMessage $OrderMessageModel)
    {
        $pagesize = $request->input('limit'); // 每页条数
        $page = $request->input('page',1);//当前页
        $order_no = $request->input('order_no');
        $nickname = $request->input('nickname');
        $where = [
            ['order.order_no', 'like', '%'.$order_no.'%'],
            ['users.nickname', 'like', '%'.$nickname.'%'],
            ['order_message.is_delete', '=', '0'],
        ];

        //获取总条数
        $count = $OrderMessageModel
            ->leftJoin('order', 'order.id', '=', 'order_message.order_id')
            ->leftJoin('users', 'users.id', '=', 'order.user_id')
            ->where($where)
            ->count();
        
        //求偏移量
        $offset = ($page-1)*$pagesize;
        $list = $OrderMessageModel
                ->leftJoin('order', 'order.id', '=', 'order_message.order_id')
                ->leftJoin('users', 'users.id', '=', 'order.user_id')   
                ->where($where)
                ->select('order_message.*','users.nickname','users.avatar','order.order_no')
                ->orderByDesc('id')->offset($offset)->limit($pagesize)->get();
        foreach($list as $key => $v){
            $list[$key]['order_type'] = $OrderMessageModel->getOrderType($v['order_type']);
        }
        return $this->success(compact('list', 'count'));    
        
    }

    /**
     * 删除
     */
    public function delete(Request $request, OrderMessage $OrderMessageModel){
        if($request->isMethod('post')){
            $id = $request->input('id');
            if($id){
                $reulst = $OrderMessageModel->where('id',$id)->update(['is_delete'=>1]);
                if($reulst){
                    return $this->success('操作成功');
                }else{
                    return $this->failed('操作失败');
                }
            }else{
                return $this->failed('缺少参数id');
            }
        }
    }

    /**
     * 批量删除
     */
    public function batchDelete(Request $request, OrderMessage $OrderMessageModel){
        if($request->isMethod('post')){
            $id = $request->input('id');
            if($id){
                $reulst = $OrderMessageModel->whereIn('id',$id)->update(['is_delete'=>1]);
                if($reulst){
                    return $this->success('操作成功');
                }else{
                    return $this->failed('操作失败');
                }
            }else{
                return $this->failed('请勾选要删除的数据');
            }
        }
    }

    /**
     * 标为已提醒
     */
    public function soundAll(Request $request, OrderMessage $OrderMessageModel)
    {
        if($request->isMethod('post')){
            
            $list = $OrderMessageModel->where([['is_read','=',0],['is_delete','=',0]])->get();
            $id = [];
            foreach($list as $key => $v){
                $id[] = $v['id'];
            }
            
            $reulst = $OrderMessageModel->whereIn('id',$id)->update(['is_read'=>1]);
            if($reulst){
                return $this->success('操作成功');
            }else{
                return $this->failed('操作失败');
            }
        }else{
            return $this->failed('请求错误');
        }
    }
}